#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

void snow(Mat &gray)
{
    Mat tmp_m;
    Mat tmp_sd;
    double m = 0, sd = 0;
    meanStdDev(gray, tmp_m, tmp_sd);
    m = tmp_m.at<double>(0, 0);
    sd = tmp_sd.at<double>(0, 0);
    std::cout << "StdDev: " << sd * sd << std::endl; //æ–¹å·®

    if ((sd * sd) > 1)
        printf("雪花\n");
}

int main() //snow
{
    //读入视频
    VideoCapture capture("/Users/lukeskywalker/Desktop/snowscreen.mov");
    //VideoCapture capture(0);
    //namedWindow("camera", WINDOW_AUTOSIZE);
    //namedWindow("moving area", WINDOW_AUTOSIZE);
    //获取整个帧数

    Mat frame;
    Mat gray;
    Mat tempframe;
    Mat medianblur;
    Mat currentframe;
    int framenum = 0;
    //读取一帧处理
    while (true)
    {
        if (!capture.isOpened())
        {
            cout << "read video failure" << endl;
            return -1;
        }
        //tempframe = capture.read(frame);

        //获取整个帧数
        //long totalFrameNumber = capture.get(CV_CAP_PROP_FRAME_COUNT);
        //cout << "整个视频共" << totalFrameNumber << "帧" << endl;

        capture >> frame;
        tempframe = frame;
        framenum++;
        if (framenum == 1)
        {
            cvtColor(tempframe, gray, CV_BGR2GRAY);
            medianBlur(gray, medianblur, 3);
            absdiff(gray, medianblur, currentframe); //做差求绝对值
        }
        if (framenum >= 2)
        {
            cvtColor(tempframe, gray, CV_BGR2GRAY); //转化为单通道灰度图，此时currentFrame已经存了tempFrame的内容
            medianBlur(gray, medianblur, 3);
            absdiff(gray, medianblur, currentframe); //做差求绝对值

            //显示图像
            //imshow("camera", tempframe);
            //imshow("moving area", gray);
        }
        if (!(framenum % 1))
        {
            printf("f:%d\n", framenum);
            //***suanfa;
        }

        snow(currentframe); //做差求绝对值);
        imshow("video", frame);

        if (cvWaitKey(10) == 27)
            break;
    }
    return 0;
}
